<?php
namespace app\admin\controller;
use think\Db;
use org\transform\driver\Json;
class User extends Base
{
    /**
     * 会员列表
     * @return \think\response\View
     */
	public function lists(){
        if( is_post() ){
            $page     = input('page');
            $status   = input('status');
            $level    = input('level');
            $type     = input('type');
            $keyword  = input('keyword');
            $min_time = input('min_time');
            $max_time = input('max_time');
            
            //原生态sql查询
            $sql = "
                select
                    [1]
                from
                    user
                where 
                    1 = ?
                ";
            $param = [1];
            if ( $status != '100all' ) {
                $sql .= " and status = ?";
                $param[] = $status;
            }
            if ( $level != '100all' ) {
                $sql .= " and level = ?";
                $param[] = $level;
            }
            if ( !empty($keyword) ) {
                $sql .= " and (name like ?)";
                $param[] = "%{$keyword}%";
            }
            if ( !empty($min_time) ) {
                $sql    .= " and create_time >= ?";
                $param[] = strtotime($min_time);
            }
            if ( !empty($max_time) ) {
                $sql    .= " and create_time <= ?";
                $param[] = strtotime($max_time);
            }
            //总条数
            $sql_count = str_replace("[1]", "count(*) as total", $sql);
            $total     = Db::query($sql_count, $param);
            $total     = $total[0]['total'];
            
            //分页数据
            $page_size   = c('page_size');     #分页条数
            $page        = com_cur_page($total, $page,  $page_size);   #当前页
			$page_data   = com_page_data($total, $page,  $page_size);   #分页原始数据
            
            //列表数据
			$start       = ($page - 1) * $page_size;
			$sql        .= " order by id desc limit $start, $page_size";
			$field       = "*";
			$sql_list    = str_replace("[1]", $field, $sql);
            $list        = Db::query($sql_list, $param);
             
            $ids = [];
            foreach($list as $v){
                $ids[] = $v['parent_id'];
            }
            if($ids){
                $rs = Db::name('user')->field('id, name')->where('id','in', $ids)->select();
            }
            
            //格式数据
            foreach($list as $k => $v){           
                $list[$k]['checkbox'] =  com_format_checkbox($v['id']);     
                $list[$k]['status']   = c('type.user_status', $v['status']);
                $list[$k]['level']    = c('type.user_level', $v['level']);
                $list[$k]['headimgurl']  =  com_format_pic($v['headimgurl'],30);
                $list[$k]['create_time'] =  com_format_time( $v['create_time'] );
                $list[$k]['active_time'] =  com_format_time( $v['active_time'] );
                
                $list[$k]['parent_id'] =  "";
                foreach($rs as $val){
                    if($val['id']==$v['parent_id'])
                    $list[$k]['parent_id'] =  $val['name'];
                }
                
                //操作按钮
                $list[$k]['action']         =  com_action_edit( url("user/edit","id={$v['id']}"), $v['id'], '编辑用户' );
            }
			$com_body    = com_build_table($list, array("id","headimgurl","name","province","city","level","parent_id","money","points","create_time","status", "active_time", "action"));
			
			$com_page    = com_build_page( $page_data );
			$data = array(
				"com_body"  => $com_body,
				"com_page"  => $com_page,
			);
            $config = [
                'data'   => $data,
            ];
			com_check($config);
        }
        $data = [
            'nav'       => ['会员管理', '列表'],
            'status'    => ['100all'=>'全部状态'] + c('type.user_status'),
            'level'     => ['100all'=>'全部等级'] + c('type.user_level'),
        ];
        return view('', $data);
    }
    
    
    //编辑
    public function edit( $id ){
        if( is_post() ){
            $data = input();  #post数据
            
            $data['id'] = $id;
            \app\admin\model\User::update($data);
            $config = [
                'suc' => [
                    'msg' => '编辑会员成功！',
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        }
        
        $info   = \app\admin\model\User::get($id)->getData();
        $data = [
            'level'    => c('type.user_level'),
            'info'      => $info,
        ];
        return view('', $data);
    }


    //编辑财务
    public function editMoney( $id ){
        if( is_post() ){
            $user_id     = input('id');
            $money_type  = input('money_type');
            $money       = (float)input('money');
            $note        = input('note');
            //验证
            $rule = [
                'money'       =>  'require',
            ];
            $msg = [
                'money.require'     =>  '金额不能为空',
            ];
            $data = [
                'money' => $money,
            ];
            com_validate( $rule, $msg, $data, true );
            
            $h = new \app\admin\model\HandMoney();
            if($money<0){
                $money = bcsub(0, $money,2);
                $h->remove($user_id, $money_type, $money, $note);
            }else{
                $money = bcadd(0, $money,2);
                $h->add($user_id, $money_type, $money, $note);
            }
            $config = [
                'suc' => [
                    'msg' => '编辑用户财务成功！',
                    'js_action' => 'alert|reload',
                ],
            ];
            com_check( $config );
        }
        //模板赋值
        $info   = Db::name('user')->find($id);
        $data = [
            'info'      => $info,
            'money_type'=> c('type.self_money_type'),
        ];
        return view('', $data);
    }
}